package com.white.server.impl;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

public class Client {
    public static Socket clientSocket;
    public static Boolean IS_CONNECTION;

    private static void connection() {
        try {
            clientSocket = new Socket("localhost", 8080);
            IS_CONNECTION = true;
            ObjectOutputStream outputStream = new ObjectOutputStream(clientSocket.getOutputStream());
            ObjectInputStream inputStream = new ObjectInputStream(clientSocket.getInputStream());
            new Thread(new ClientListen(inputStream)).start();
            new Thread(new ClientSend(clientSocket, outputStream)).start();
            new Thread(new ClientHeart(clientSocket, outputStream)).start();
        } catch (IOException e) {
            e.printStackTrace();
            IS_CONNECTION = false;
        }

    }

    public static void reConnection() {
        while (!IS_CONNECTION) {
            System.out.println("断线重连中...");
            try {
                clientSocket.close();
                IS_CONNECTION = false;
                connection();
                Thread.sleep(3000);
            } catch (InterruptedException | IOException e) {
                e.printStackTrace();
            }
        }
    }
    public static void main(String[] args) {
        connection();
    }
}
